package com.jwell.panrank.tools;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

/**
 * 状态栏设置工具
 */

public class StatuseBarUtils {
    private static int DEFAULTHEIGHT = 60;
    private static int STATUTSTOOLHEIGHT = -1;


    //5.0以上的处理
    public static void setStatusBarColor(int color, Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //如果是6.0以上将状态栏文字改为黑色，并设置状态栏颜色
//            activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            activity.getWindow().setStatusBarColor(color);
            //fitsSystemWindow 为 false, 不预留系统栏位置.
            ViewGroup mContentView = (ViewGroup) activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
            View mChildView = mContentView.getChildAt(0);
            if (mChildView != null) {
                ViewCompat.setFitsSystemWindows(mChildView, true);
                ViewCompat.requestApplyInsets(mChildView);
            }
        }
    }


    //通过系统尺寸资源获取状态栏高度
    public static int getSTATUSTOOLHEIGHT(Context context) {
        //获取status_bar_height资源的ID
        if (context != null) {
            try {
                int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    //根据资源ID获取响应的尺寸值
                    STATUTSTOOLHEIGHT = context.getResources().getDimensionPixelSize(resourceId);
                    System.out.println("系统状态栏高度为:" + STATUTSTOOLHEIGHT);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("默认态栏高度为:" + DEFAULTHEIGHT);
        }
        if (STATUTSTOOLHEIGHT != -1) {
            return STATUTSTOOLHEIGHT;
        } else {
            return DEFAULTHEIGHT;
        }
    }
}